<?php
namespace Seller\Controller;
use Seller\Logic\OrderLogic;
use Think\AjaxPage;
use Think\Page;

class UserController extends BaseController {

    public function index(){
        $this->display();
    }

    /**
     * 会员列表
     */
    public function ajaxindex(){
        // 搜索条件
        $condition = array();
        I('mobile') ? $condition['mobile'] = I('mobile') : false;
        I('email') ? $condition['email'] = I('email') : false;
        $sort_order = I('order_by').' '.I('sort');
        //找到后台分配给这个商家的用户
        $userArr = M('store_floating')->where(['store_id'=>STORE_ID])->getField('user_id',true);
         if(!empty($userArr)){
             $condition['user_id'] = ['in',implode(',',$userArr)];
         }else{
             $condition['user_id'] = 0;
         }
        $model = M('users');
        $count = $model->where($condition)->count();
        $Page  = new AjaxPage($count,10);
        //  搜索条件下 分页赋值
        foreach($condition as $key=>$val) {
            $Page->parameter[$key]   =   urlencode($val);
        }
//        dump($condition);
        $userList = $model->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
                
        $user_id_arr = get_arr_column($userList, 'user_id');
        if(!empty($user_id_arr))
        {
            $first_leader = M('users')->query("select first_leader,count(1) as count  from __PREFIX__users where first_leader in(".  implode(',', $user_id_arr).")  group by first_leader");
            $first_leader = convert_arr_key($first_leader,'first_leader');
            
            $second_leader = M('users')->query("select second_leader,count(1) as count  from __PREFIX__users where second_leader in(".  implode(',', $user_id_arr).")  group by second_leader");
            $second_leader = convert_arr_key($second_leader,'second_leader');            
            
            $third_leader = M('users')->query("select third_leader,count(1) as count  from __PREFIX__users where third_leader in(".  implode(',', $user_id_arr).")  group by third_leader");
            $third_leader = convert_arr_key($third_leader,'third_leader');            
        }
        $this->assign('first_leader',$first_leader);
        $this->assign('second_leader',$second_leader);
        $this->assign('third_leader',$third_leader);
                                
        $show = $Page->show();
        $this->assign('userList',$userList);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    /**
     * 会员详细信息查看
     */
    public function detail(){
        $uid = I('get.id');
        $user = D('users')->where(array('user_id'=>$uid))->find();
        if(!$user)
            exit($this->error('会员不存在'));
        if(IS_POST){
            //  会员信息编辑
            $password = I('post.password');
            $password2 = I('post.password2');
            if($password != '' && $password != $password2){
                exit($this->error('两次输入密码不同'));
            }
            if($password == '' && $password2 == ''){
                unset($_POST['password']);
            }else{
                $_POST['password'] = encrypt($_POST['password']);
            }

            $row = M('users')->where(array('user_id'=>$uid))->save($_POST);
            if($row)
                exit($this->success('修改成功'));
            exit($this->error('未作内容修改或修改失败'));
        }
        
        $user['first_lower'] = M('users')->where("first_leader = {$user['user_id']}")->count();
        $user['second_lower'] = M('users')->where("second_leader = {$user['user_id']}")->count();
        $user['third_lower'] = M('users')->where("third_leader = {$user['user_id']}")->count();
 
        $this->assign('user',$user);
        $this->display();
    }

    /**
     * 用户收货地址查看
     */
    public function address(){
        $uid = I('get.id');
        $lists = D('user_address')->where(array('user_id'=>$uid))->select();
        $regionList = M('Region')->getField('id,name');
        $this->assign('regionList',$regionList);
        $this->assign('lists',$lists);
        $this->display();
    }

    /**
     * 删除会员
     */
    public function delete(){
        $uid = I('get.id');
        $row = M('users')->where(array('user_id'=>$uid))->delete();
        if($row){
            $this->success('成功删除会员');
        }else{
            $this->error('操作失败');
        }
    }

    /**
     * 账户资金记录
     */
    public function account_log(){
        $user_id = I('get.id');
        //获取类型
        $type = I('get.type');
        //获取记录总数
        $count = M('account_log')->where(array('user_id'=>$user_id))->count();
        $page = new Page($count);
        $lists  = M('account_log')->where(array('user_id'=>$user_id))->order('change_time desc')->limit($page->firstRow.','.$page->listRows)->select();

        $this->assign('user_id',$user_id);
        $this->assign('page',$page->show());
        $this->assign('lists',$lists);
        $this->display();
    }

    /**
     * 账户资金调节
     */
    public function return_goods(){        

//        $desc = I('post.desc');

        $return_goods_id = I('return_goods_id');
        $return_goods = M('return_goods')->where("id = $return_goods_id and store_id = ".STORE_ID)->find();
        empty($return_goods) && $this->error("参数有误");
        
        $user_id = $return_goods['user_id'];
       $pay_code = M('order')->where("order_id = {$return_goods['order_id']}")->getField('pay_code');
       if ($pay_code == 'weixin'){
           $desc = "退款到微信余额";
       }
        $order_goods = M('order_goods')->where("order_id ={$return_goods['order_id']} and goods_id = {$return_goods['goods_id']} and spec_key = '{$return_goods['spec_key']}'")->find();

//        if($order_goods['is_send'] != 1)
//
//            $this->error("该订单商品状态不能退款操作");

/*
        $order = M('order')->where("order_id = {$return_goods['order_id']}")->find();
        
        
        // 计算退回积分公式
        //  退款商品占总商品价比例 =  (退款商品价 * 退款商品数量)  / 订单商品总价      // 这里是算出 退款的商品价格占总订单的商品价格的比例 是多少
        //  退款积分 = 退款比例  * 订单使用积分
         
        // 退款价格的比例        
        $return_price_ratio = ($order_goods['member_goods_price'] * $order_goods['goods_num']) / $order['goods_price'];
        // 退还积分 = 退款价格的比例 * 
        $return_integral = ceil($return_price_ratio * $order['integral']);        
        
         // 退还金额 = (订单商品总价 - 优惠券 - 优惠活动) * 退款价格的比例 - (退还积分 + 当前商品送出去的积分) / 积分换算比例    
         // 因为积分已经退过了, 所以退金额时应该把积分对应金额推掉 其次购买当前商品时送出的积分也要退回来,以免被刷积分情况
                       
        $return_goods_price = ($order['goods_price'] - $order['coupon_price'] - $order['order_prom_amount']) * $return_price_ratio - ($return_integral + $order_goods['give_integral']) /  tpCache('shopping.point_rate');
        $return_goods_price = round($return_goods_price,2); // 保留两位小数
 */       
        
        $refund = order_settlement($return_goods['order_id'],$order_goods['rec_id']);  // 查看退款金额        
      //  print_r($refund);
        $return_goods_price = $refund['refund_settlement'] ? $refund['refund_settlement'] : 0; // 这个商品的退款金额
        //$refund_integral = $refund['refund_integral'] ? ($refund['refund_integral'] * -1) : 0; // 这个商品的退积分
        $refund_integral = $refund['refund_integral'] - $refund['give_integral'];
        
        
        if(IS_POST)
        {            

            if(!$_POST['desc'])

                $this->error("请填写操作说明");
            if(!$user_id > 0)
                $this->error("参数有误");
                        
            $pending_money = M('store')->where(" store_id = ".STORE_ID)->getField('pending_money'); // 商家在未结算资金 
            if($pending_money < $return_goods_price)
                $this->error("你的未结算资金不足 ￥{$return_goods_price}");

            //根据订单提交的方式进行退款
            if($pay_code == 'balance'){
                //从商家未结算资金中扣钱，并且记录商家账户日志
                $result = storeAccountLog(STORE_ID,0,$return_goods_price * -1,'退款',$order_goods['order_id']);
                //客户支付方式为余额支付
                if($result)
                {
                    accountLog($user_id,$return_goods_price,$refund_integral,'订单退款',0,$return_goods['order_id']);
                }  else {
                    $this->error("退款失败");
                }
            }else{
                //客户支付方式为微信支付
                $order_id = $order_goods['order_id'];
                $order_list = M('order')->where("order_id = {$order_goods['order_id']}")->select();
                $order_sn = $order_list[0]['master_order_sn'];
                $money_paid =$order_list[0]['total_amount'];
				
				
                require_once(PLUGIN_PATH.'payment/weixin/wxrefund.php');
				
                require_once(PLUGIN_PATH.'payment/weixin/lib/WxPay.Api.php');
                $wx_refund_status=do_wx_refund($order_id,$order_sn,$money_paid,$return_goods_price);
                if ($wx_refund_status){
                    accountLog($user_id,0,$refund_integral,'订单退款',0,$return_goods['order_id']);
                    echo 'success';
                } else {
                    $this->error("退款失败");
                }

            }


            M('order_goods')->where("rec_id = {$order_goods['rec_id']}")->save(array('is_send'=>3));//更改商品状态
            // 如果一笔订单中 有退货情况, 整个分销也取消
            M('rebate_log')->where("order_id = {$return_goods['order_id']}")->save(array('status'=>4,'remark'=>'订单有退货取消分成'));


            $this->success("操作成功",U("Order/return_info",array('id'=>$return_goods_id)));

            exit;
        }
                
        $this->assign('return_goods_price',$return_goods_price);
        $this->assign('return_integral',$refund_integral);
        $this->assign('order_goods',$order_goods);
        $this->assign('user_id',$user_id);
        $this->assign('desc',$desc);

        $this->display();
    }
    
    public function level(){
    	$act = I('GET.act','add');
    	$this->assign('act',$act);
    	$level_id = I('GET.level_id');
    	$level_info = array();
    	if($level_id){
    		$level_info = D('user_level')->where('level_id='.$level_id)->find();
    		$this->assign('info',$level_info);
    	}
    	$this->display();
    }
    
    public function levelList(){
    	$Ad =  M('user_level');
    	$res = $Ad->where('1=1')->order('level_id')->page($_GET['p'].',10')->select();
    	if($res){
    		foreach ($res as $val){
    			$list[] = $val;
    		}
    	}
    	$this->assign('list',$list);
    	$count = $Ad->where('1=1')->count();
    	$Page = new \Think\Page($count,10);
    	$show = $Page->show();
    	$this->assign('page',$show);
    	$this->display();
    }
    
    public function levelHandle(){
    	$data = I('post.');
    	if($data['act'] == 'add'){
    		$r = D('user_level')->add($data);
    	}
    	if($data['act'] == 'edit'){
    		$r = D('user_level')->where('level_id='.$data['level_id'])->save($data);
    	}
    	 
    	if($data['act'] == 'del'){
    		$r = D('user_level')->where('level_id='.$data['level_id'])->delete();
    		if($r) exit(json_encode(1));
    	}
    	 
    	if($r){
    		$this->success("操作成功",U('Admin/User/levelList'));
    	}else{
    		$this->error("操作失败",U('Admin/User/levelList'));
    	}
    }

    /**
     * 搜索用户名
     */
    public function search_user()
    {
        $search_key = trim(I('search_key'));
        $store_id = trim(I('store_id'));
        $dai_type = trim(I('dai_type'));
//			if($dai_type=='1'){
//				$where="(mobile like'%$search_key%' or company like'%$search_key%') and is_lock=0 and store_id=".$store_id;
//			}else{
				$where="(mobile like'%$search_key%' or company like'%$search_key%' or nickname like'%$search_key%') and is_lock=0 ";
//			}
        $list = M('users')->where($where)->select();
        
			
//            $list = M('users')->where($where)->select();
			if(empty($list)){
				echo \json_encode([
					'code'=>-1
				]);
				return false;
			}

			foreach($list as $v){
				$list_s.='<li rel="'.$v['user_id'].'" rev="'.$v['mobile'].'" alt="'.$v['nickname'].'" class="'.$v['company_address'].'" title="'.$v['company'].'" id="'.$v['biaoshi_xian'].'">'.$v['company'].'：'.$v['company_address'].'('.$v['nickname'].')</li>';
			}
				echo \json_encode([
					'code'=>'200',
					'selects'=>$list_s
				]);
        exit;
    }
	
    
    /**
     * 分销树状关系
     */
    public function ajax_distribut_tree()
    {
          $list = M('users')->where("first_leader = 1")->select();
          $this->display();
    }


    /**
     * 用户产品浮动价格
     * */
    public function search_goods_id(){


        $where = ' is_on_sale = 1  and store_id ='.STORE_ID;//搜索条件and b.prom_type=0
        $keywords = I('keywords');
        if(!empty($keywords)){
            $where .= ' and (goods_name like "%'.$keywords.'%")';
        }
        $count = M('goods')->where($where)->count();
        $Page  = new \Think\Page($count,30);
        $goodsList = M('goods')->where($where)->order('goods_id DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
        $show = $Page->show();//分页显示输出
        $this->assign('page',$show);//赋值分页输出
        $this->assign('goodsList',$goodsList);
        $this->assign('keywords',$keywords);
        $this->display();
    }

    /**
     * 查看这个用户的商品浮价
     * */
    public function user_goods(){
        $user_id = I('user_id');
        $store_id = STORE_ID;
        $userInfo = M('store_floating')->where('user_id ='.$user_id.' and store_id ='.$store_id)->find();

        if($userInfo['goods_ids']){
            $userInfo['goods_ids'] = json_decode($userInfo['goods_ids'],true);
            foreach ($userInfo['goods_ids'] as $key=>$v){
                $userInfo['goods_ids'][$key]['goods_name'] = M('goods')->where('goods_id ='.$v['goods_id'])->getField('goods_name');
                $userInfo['goods_ids'][$key]['original_img'] = M('goods')->where('goods_id ='.$v['goods_id'])->getField('original_img');
            }
        }


        $this->assign('info',$userInfo);
        $this->display();
    }


    /**
     * 保存
     * */
    public function user_goods_save(){
        if(IS_POST){
            $user_id = I('user_id');
            $store_id = STORE_ID;
            $floating = I('floating');
            $goods_ids = I('goods_id');
            $data = [];
            foreach($goods_ids as $key=>$v){
                $data[] = ['goods_id'=>$v,'floating'=>$floating[$key]?$floating[$key]:0];
            }
            //查询是否存在
            $store_floating = M('store_floating')->where('user_id ='.$user_id.' and store_id ='.$store_id)->find();
            if($store_floating){
                $r = M('store_floating')->where('id ='.$store_floating['id'])->save(['goods_ids'=>json_encode($data)]);
            }else{
                $r = M('store_floating')->add(['user_id'=>$user_id,'store_id'=>$store_id,'goods_ids'=>json_encode($data)]);
            }
            if($r){
                $this->success("操作成功",U('Seller/User/user_goods','user_id='.$user_id));
            }else{
                $this->error("操作失败",U('Seller/User/user_goods','user_id='.$user_id));
            }
        }
    }

    /**
     * 查看这个用户的品牌浮价
     * */
    public function user_brand(){
        $user_id = I('user_id');
        $store_id = STORE_ID;
        $userInfo = M('store_floating')->where('user_id ='.$user_id.' and store_id ='.$store_id)->find();

        if($userInfo['brand_id']){
            $userInfo['brand_id'] = json_decode($userInfo['brand_id'],true);
            foreach ($userInfo['brand_id'] as $key=>$v){
                $userInfo['brand_id'][$key]['brand_name'] = M('brand')->where('id ='.$v['brand_id'])->getField('name');
            }
        }
//        dump($userInfo);
        $this->assign('info',$userInfo);
        $this->display();
    }

    /**
     * 用户产品浮动价格
     * */
    public function search_brand_id(){

        $where = '1=1';
        $keywords = I('keywords');
        if(!empty($keywords)){
            $where .= ' and (name like "%'.$keywords.'%")';
        }
        $count = M('brand')->where($where)->group('name')->select();
        $Page  = new \Think\Page(count($count),30);

        $brandList = M('brand')->where($where)->order('id DESC')->limit($Page->firstRow.','.$Page->listRows)->group('name')->select();

        $show = $Page->show();//分页显示输出
        $this->assign('page',$show);//赋值分页输出
        $this->assign('brandList',$brandList);
        $this->assign('keywords',$keywords);
        $this->display();
    }

    /**
     * 保存
     * */
    public function user_brand_save(){
        if(IS_POST){
            $user_id = I('user_id');
            $store_id = STORE_ID;
            $floating = I('floating');
            $brand_ids = I('brand_id');
            $data = [];
            foreach($brand_ids as $key=>$v){
                $data[] = ['brand_id'=>$v,'floating'=>$floating[$key]?$floating[$key]:0];
            }

            //查询是否存在
            $store_floating = M('store_floating')->where('user_id ='.$user_id.' and store_id ='.$store_id)->find();
            if($store_floating){
                $r = M('store_floating')->where('id ='.$store_floating['id'])->save(['brand_id'=>json_encode($data)]);
            }else{
                $r = M('store_floating')->add(['user_id'=>$user_id,'store_id'=>$store_id,'brand_id'=>json_encode($data)]);
            }
            if($r){
                $this->success("操作成功",U('Seller/User/user_brand','user_id='.$user_id));
            }else{
                $this->error("操作失败",U('Seller/User/user_brand','user_id='.$user_id));
            }
        }
    }

}